#!/bin/sh

cd /tmp
rm -rf hcn-test.zip hcn-test.sql

TOKEN=$1
PASSWORD=$2
TENANTID=$3
if [ ! -n "$PASSWORD" ] ;then
    PASSWORD="atagbsoft"
fi

if [ "$TENANTID" == "hcn.test" ] ;then
    echo "请设置租户ID"
    exit
fi

echo "TOKEN:$TOKEN \n"
echo "PASSWORD:$PASSWORD \n"
echo "TENANTID:$TENANTID \n"

# 将文件保存到本地
curl -O --header "PRIVATE-TOKEN: $TOKEN" "http://jkcs.bsoft.com.cn/bs-standard-repo/ops/install/raw/master/hcn-test.zip" 

unzip hcn-test.zip

# 创建数据库及用户
echo "创建数据库...开始"
mysql -uroot -p$PASSWORD <<EOF
CREATE DATABASE IF NOT EXISTS hcn DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL ON hcn.* TO 'atag'@'%' IDENTIFIED BY 'bsoft@123';
flush privileges;
EOF
echo "创建数据库...结束"

echo "还原数据库...开始"
mysql -uroot -p$PASSWORD -D hcn </tmp/hcn-test.sql

echo "还原数据库...结束"

echo "更新数据库租户...开始"
mysql -uroot -p$PASSWORD -D hcn <<EOF
CALL update_tenantId('hcn.zhongshan','$TENANTID');
EOF
echo "更新数据库租户...结束"